package com.fmsz.xmly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.fmsz.xmly.R;
import com.fmsz.xmly.entitys.anchor.AdvertiseEntity;
import com.fmsz.xmly.utils.Urls;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class AdvertiseActivity extends AppCompatActivity {
    private ImageView imageView;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            startActivity(new Intent(AdvertiseActivity.this,HomeActivity.class));
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advertise);
        init();

    }

    private void init() {
        imageView = (ImageView) findViewById(R.id.advertise_iv);
        handler.sendEmptyMessageDelayed(1, 3*1000);
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, Urls.ADVERTISE, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {//这个反应的方法里面进行解析和ui控件的值得设定
                try {
                    JSONArray array = new JSONObject(response).getJSONArray("data");
                    //fastjson解析数组里的对象
                    List<AdvertiseEntity> list = JSON.parseArray(array.toString(), AdvertiseEntity.class);
                    int random= (int) (Math.random()*list.size()-1);
                    Picasso.with(AdvertiseActivity.this).load(list.get(random).getCover()).into(imageView);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, null);
        requestQueue.add(stringRequest);

    }


}
